

#define HEIGHTMAP_PATCH_SIZE 16


struct wgHeightmapTexture
{
	wgImage image;
	int		min, optimal, max;
};


struct wgHeightmapData
{
	int						value;
	vector3_t				normal;
	bool					visible;
};


struct wgHeightmapPatch
{
	bool visible;
	int x, z;
	int width, height;	
	wgHeightmapPatch* subPatches[4];

};


class wgHeightmap
{
	wgHeightmapData* data;

	wgHeightmapPatch rootPatch;


	vector<wgHeightmapTexture*>	textures;

	int width, height;
	float sampleX;
	float sampleY;
	float sampleZ;

	void CalculateVertexNormals();
	void DividePatches();
	void LoadTextures(string&);
	void DrawHeightmapVertex(int x, int z);


	void DivideHeightmapPatch(int x, int y, int width, int height, wgHeightmapPatch *pPatch);
	void DestroySubPatches(wgHeightmapPatch*p);

	void UpdatePatches(wgCamera& cam, wgHeightmapPatch* pPatch);
	void Render(int x, int z, int sX, int sZ);
	void RenderPatch(wgHeightmapPatch* p);



public:
	wgHeightmap();
	~wgHeightmap();
	bool Load(string& filename);
	int getWidth();
	int getHeight();
	void setSampleSize(float, float, float);
	float getMapSizeX();
	float getMapSizeY();
	float getMapSizeZ();

	int getHeightValue(int x, int z);
	float getSampledHeight(float x, float z);

	void Update(wgCamera& c);
	void Render();

	void BindTextures();
	
	wgHeightmapData* getDataPtr(int x, int y);

};